package com.example.handle;

import com.example.common.Result;
import com.example.exception.NullException;
import com.example.exception.ParamException;
import com.example.exception.TokenException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author itheima
 */
// @RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public Result runtimeHandle(RuntimeException e) {
        log.error(e.getMessage());
        return Result.error("出现了运行时异常");
    }

    @ResponseBody
    @ExceptionHandler(TokenException.class)
    public Result tokenHandle() {
        return Result.error("token异常");
    }

    @ResponseBody
    @ExceptionHandler(NullException.class)
    public Result nullHandle(NullException e) {
        log.error(e.getMessage());
        return Result.error("对象为空");
    }

    @ResponseBody
    @ExceptionHandler(ParamException.class)
    public Result paramHandle(ParamException e) {
        log.error(e.getMessage());
        return Result.error("入参为空");
    }
}
